﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Newtonsoft.Json;

public class GUIDependencyConfig {

    private static GUIDependencyConfig instance;
    private GUIDependencyRoot root;

    public static GUIDependencyConfig Instance()
    {
        if (instance == null)
        {
            instance = new GUIDependencyConfig();
        }
        return instance;
    }

    private GUIDependencyConfig()
    {

    }

    public void Init()
    {
        string path = ResourcePath.GUIDependencyPath;

        if (!string.IsNullOrEmpty(path))
        {
            using (StreamReader streamReader = new StreamReader(path))
            {
                string content = streamReader.ReadToEnd();
                root = JsonConvert.DeserializeObject<GUIDependencyRoot>(content);
            }
        }
    }

    public List<string> GetDependencies(string prefab)
    {
        for (int i = 0; i < root.uiDependencyItemList.Count; ++i)
        {
            if (prefab.Equals(root.uiDependencyItemList[i].prefabPath))
            {
                return root.uiDependencyItemList[i].atlasPath;
            }
        }

        return null;
    }

    private class GUIDependencyRoot
    {
        public string rootPath;
        public List<GUIDependencyItem> uiDependencyItemList;
    }

    private class GUIDependencyItem
    {
        public string prefabPath;
        public List<string> atlasPath;
    }

}
